<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-input v-decorator="['id']" hidden="true"></a-input>
        <a-input v-decorator="['param']" hidden="true"></a-input>
        <a-input v-decorator="['brand']" hidden="true"></a-input>
        <a-row>
          <a-col :span="24">
            <a-form-item label="资产分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['materialCode']" placeholder="请输入资产分类" readOnly @click="onSearchMmsPurchaseList">
                <a-icon slot="prefix" type="bars"/>
              </a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="资产名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['materialName']" placeholder="请输入资产名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['specs']" placeholder="请输入规格"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['model']" placeholder="请输入型号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="计量单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['unit']" placeholder="请输入计量单位"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="预估数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['orderNum']" placeholder="请输入预估数量" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="单价" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['price', validatorRules.price]" placeholder="请输入单价" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
    <mms-purchase-list-modal ref="mmsPurchaseListModel" @ok="onMmsPurchaseListOk"></mms-purchase-list-modal>
    <mms-material-unit-modal ref="mmsMaterialUnitModal" @ok="onMmsMaterialUnitListOk"></mms-material-unit-modal>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@lone/lone-vue/src/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@lone/lone-vue/src/utils/util'
  import JFormContainer from '@lone/lone-vue/src/components/jeecg/JFormContainer'
  import JDate from '@lone/lone-vue/src/components/jeecg/JDate'
  import MmsPurchaseListModal from './MmsPurchaseListModal'
  import MmsMaterialUnitModal from './MmsMaterialUnitModal'
  import Vue from 'vue'

  export default {
    name: 'MmsContractAssetForm',
    components: {
      JFormContainer,
      JDate,
      MmsPurchaseListModal,
      MmsMaterialUnitModal,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules:{
          /*price: {rules: [{required: true, message: '请输入单价', trigger: 'change' }]}*/
        },
        url: {
          add: "/cg/mmsWzPurchaseOrderDetails/add",
          edit: "/cg/mmsWzPurchaseOrderDetails/edit",
          queryById: "/cg/mmsWzPurchaseOrderDetails/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      onSearchMmsMaterialUnitList(){
        this.$refs.mmsMaterialUnitModal.showMmsMaterialUnitInfo('ok');
      },
      onMmsMaterialUnitListOk(selectionRows, num){
        this.form.setFieldsValue({unit: selectionRows.name})
      },
      onSearchMmsPurchaseList(){
        this.$refs.mmsPurchaseListModel.showMmsMaterialInfo('ok');
      },
      onMmsPurchaseListOk(selectionRows, num){
        this.form.setFieldsValue({materialCode: selectionRows.code})
        this.form.setFieldsValue({materialName: selectionRows.name})
        this.form.setFieldsValue({specs: selectionRows.specs})
        this.form.setFieldsValue({model: selectionRows.model})
        this.form.setFieldsValue({unit: selectionRows.unit})
        this.form.setFieldsValue({param: selectionRows.param})
        this.form.setFieldsValue({brand: selectionRows.brand})
        this.form.setFieldsValue({isQuality: selectionRows.isQuality})
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'id', 'materialCode','materialName','classifyCode','classifyName','model','specs','unit','price','param', 'brand', 'isQuality'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            this.$emit("ok",formData);
            that.confirmLoading = false;
          }
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'materialCode','materialName','classifyCode','classifyName','model','specs','unit','price','param', 'brand', 'isQuality'))
      },
    }
  }
</script>